package com.example.webmodule.config;

import org.apache.hc.client5.http.classic.HttpClient;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import org.apache.hc.client5.http.socket.ConnectionSocketFactory;
import org.apache.hc.client5.http.socket.PlainConnectionSocketFactory;
import org.apache.hc.client5.http.ssl.NoopHostnameVerifier;
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
import org.apache.hc.core5.http.URIScheme;
import org.apache.hc.core5.http.config.Registry;
import org.apache.hc.core5.http.config.RegistryBuilder;
import org.apache.hc.core5.ssl.SSLContexts;
import org.apache.hc.core5.ssl.TrustStrategy;
import org.apache.hc.core5.util.Timeout;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import javax.net.ssl.SSLContext;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;

/**
 * @ClassName EY-RestTemplateConfig
 * @Description com.example.webmodule.config
 * @Author menyj
 * @Date 2025/10/15
 **/
public class RestTemplateConfig {
    public static RestTemplate getRestTemplate() throws Exception {
        RestTemplate restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory());

        // 创建自定义 HttpClient
        HttpClient httpClient = HttpClients.custom()
            .setDefaultRequestConfig(RequestConfig.custom()
                    .setConnectTimeout(Timeout.ofSeconds(10))
                    .setResponseTimeout(Timeout.ofSeconds(30))
                    .build())
            .build();

        // 创建 RequestFactory
        HttpComponentsClientHttpRequestFactory requestFactory =
                new HttpComponentsClientHttpRequestFactory(httpClient);

        // 创建 RestTemplate
        RestTemplate restTemplate2 = new RestTemplate(requestFactory);

        return restTemplate2;
    }

    // SSL 配置示例
    public RestTemplate sslRestTemplate() throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {

        // 创建信任所有证书的 SSL 上下文
        TrustStrategy acceptingTrustStrategy = (certificate, authType) -> true;

        // 创建 SSL 上下文（信任所有证书 - 仅用于测试环境）
        SSLContext sslContext = SSLContexts.custom()
                .loadTrustMaterial(acceptingTrustStrategy)
                .build();

        // 创建 SSL socket factory 并禁用主机名验证
        SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(
                sslContext,
                NoopHostnameVerifier.INSTANCE
        );

        // 使用连接管理器配置 SSL
        Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
                .register(URIScheme.HTTPS.id, sslSocketFactory)
                .build();

        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);

        HttpClient httpClient = HttpClients.custom()
                .setConnectionManager(connectionManager)
                .build();

        // 创建 RequestFactory
        HttpComponentsClientHttpRequestFactory requestFactory =
                new HttpComponentsClientHttpRequestFactory(httpClient);

        // 创建 RestTemplate 需要 HttpComponentsClientHttpRequestFactory
        RestTemplate restTemplate = new RestTemplate(requestFactory);
        return restTemplate;
    }
}
